//529. 扫雷游戏

/**
 * @param {character[][]} board
 * @param {number[]} click
 * @return {character[][]}
 */
var updateBoard = function (board, click) {
    let xa = [-1, -1, -1, 0, 0, 1, 1, 1]
    let ya = [-1, 0, 1, -1, 1, -1, 0, 1]

    const [x, y] = click

    const updateB = (board,x,y)=>{
        let t = board[x][y]
        if (t === 'E') {
            let numMine = 0
            for (let i = 0; i < xa.length; i++) {
                let nx = x + xa[i]
                let ny = y + ya[i]
                if (nx >= 0 && nx < board.length && ny >= 0 && ny < board[0].length) {
                    if (board[nx][ny] === 'M') numMine++
                }
            }
            if (numMine === 0) {
                board[x][y] = 'B'
                for (let i = 0; i < xa.length; i++) {
                    let nx = x + xa[i]
                    let ny = x + ya[i]
                    if (nx >= 0 && nx < board.length && ny >= 0 && ny < board[0].length) {
                        updateB(board,nx, ny)
                    }
                }
            } else {
                board[x][y] = '' + numMine
            }
        }
        return
    }
    if (board[x][y] === 'M') {
        board[x][y] = 'X'
    }else{
     updateB(board,x,y)
    }
    return board
};

let board = [
    ["E", "E", "E", "E", "E"],
    ["E", "E", "M", "E", "E"],
    ["E", "E", "E", "E", "E"],
    ["E", "E", "E", "E", "E"]
]
let click = [3, 0]
updateBoard(board, click)